package com.x8.mt.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.x8.mt.common.GlobalMethodAndParams;
import com.x8.mt.common.Log;
import com.x8.mt.service.DataAnalysisService;

@CrossOrigin(origins = "*", maxAge = 3600)//解决跨域请求的注解
@Controller
@RequestMapping(value = "/dataAnalysis")
public class DataAnalysisController {
	@Resource
	DataAnalysisService dataAnalysisService;
	
	/**
	 * 
	 * 作者:allen
	 * 时间:2017年11月22日
	 * 作用:血统分析，分析targetfield字段来源
	 * 参数：id(targetfield的id)
	 */
	@RequestMapping(value = "/pedigreeAnalysis",method=RequestMethod.POST)
	@ResponseBody
	@Log(operationType="dataAnalysis",operationDesc="血统分析")
	public JSONObject pedigreeAnalysis(HttpServletRequest request,HttpServletResponse response,@RequestBody Map<String, Object> map){
		JSONObject responsejson = new JSONObject();

//		if(!GlobalMethodAndParams.checkLogin()){
//			responsejson.put("result", false);
//			responsejson.put("count",0);
//			return responsejson;
//		}
		GlobalMethodAndParams.setHttpServletResponse(request, response);
		
		//检查传参是否正确
		if(!map.containsKey("targetfieldid")){
			responsejson.put("result", false);
			responsejson.put("count",0);
			return responsejson;
		}

		String targetfieldidstr = map.get("targetfieldid").toString();
		int targetfieldid = 0;
		try {
			targetfieldid = Integer.parseInt(targetfieldidstr);
		} catch (Exception e) {
		}
		
		Map<String,Object> resultdata = dataAnalysisService.pedigreeAnalysis(targetfieldid);
		
		responsejson.put("result", true);
		responsejson.put("data", resultdata);
		responsejson.put("count",1);
		return responsejson;
	}
	
	/**
	 * 
	 * 作者:GodDispose
	 * 时间:2018年1月21日
	 * 作用:返回数据地图信息
	 */
	@RequestMapping(value = "/getDataMap",method=RequestMethod.GET)
	@ResponseBody
	@Log(operationType="sourcesystem",operationDesc="返回数据地图信息")
	public JSONObject getDataMap(HttpServletRequest request,HttpServletResponse response){
		JSONObject responsejson = new JSONObject();
		GlobalMethodAndParams.setHttpServletResponse(request, response);
		JSONObject data = dataAnalysisService.getDataMap();
		if((data.size() > 0) && data != null){
			responsejson.put("result", true);
			responsejson.put("data", data);
			responsejson.put("count",data.size());
		}else{
			responsejson.put("result", false);
			responsejson.put("count",0);
		}
		
		return responsejson;
	}
}
